/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ManageFriends.java
 *
 * Created on 10/04/2011, 17:08:11
 */

package GUI;

import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.DefaultListModel;
import objects.Frm;
import objects.User;

/**
 *
 * @author Rubik Levitsky
 */
public class ManageFriends extends javax.swing.JFrame {

    private Vector<User> _allUsers;
    private Vector<User> _friends;

    /** Creates new form ManageFriends */
    public ManageFriends(Vector<User> users, Vector<User> friends)
    {
        initComponents();
        this._allUsers = users;
        this._friends = friends;
        
        DefaultListModel usersListModel = new DefaultListModel();
        for (Iterator<User> it = _allUsers.iterator(); it.hasNext(); )
        {
            User user = it.next();  // No downcasting required.
            if(!user.getUserName() .equals(DataManager.getDataManager().getCurrUser()))
                    usersListModel.addElement(user.getUserName());
        }
        

        DefaultListModel friendsListModel = new DefaultListModel();
        for (Iterator<User> it = _friends.iterator(); it.hasNext(); )
        {
            User friend = it.next();  // No downcasting required.
            friendsListModel.addElement(friend.getUserName());
            usersListModel.removeElement(friend.getUserName()); //so users that are already friends will not appear
        }
        this.friendsList.setModel(friendsListModel);

        this.allUsers.setModel(usersListModel);

        errorLabel.setVisible(false);

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        friendsList = new javax.swing.JList();
        addFriend = new javax.swing.JButton();
        removeFriend = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        allUsers = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        back = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Manage Friends");

        friendsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(friendsList);

        addFriend.setText("<= Add User As A Friend");
        addFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addFriendActionPerformed(evt);
            }
        });

        removeFriend.setText("Remove User From Friends =>");
        removeFriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeFriendActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setForeground(new java.awt.Color(0, 204, 204));
        jLabel1.setText("Manage Friends");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setForeground(new java.awt.Color(0, 102, 255));
        jLabel2.setText("Current Friends:");

        allUsers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(allUsers);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel3.setForeground(new java.awt.Color(0, 102, 255));
        jLabel3.setText("All Users:");

        back.setText("Back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        errorLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        errorLabel.setForeground(new java.awt.Color(255, 51, 51));
        errorLabel.setText("jLabel4");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(back)
                        .addGap(111, 111, 111)
                        .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(removeFriend))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(30, 30, 30)
                                        .addComponent(addFriend)))
                                .addGap(25, 25, 25)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(47, 47, 47)))))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(118, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(addFriend)
                                        .addGap(67, 67, 67)
                                        .addComponent(removeFriend))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(225, 225, 225)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(back)
                    .addComponent(errorLabel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFriendActionPerformed
        if(!allUsers.isSelectionEmpty())
        {
            boolean alreadyAFriend = false;
            for (Iterator<User> it = _friends.iterator(); it.hasNext();)
            {
                User user = it.next();
                if(user.getUserName().equals((String)allUsers.getSelectedValue()))
                    alreadyAFriend = true;
            }
            if(!alreadyAFriend)
            {
                ButtonsFunctions.getButtonsFunctions().processAddFriend((String)allUsers.getSelectedValue());
                refresh();
            }
            else
            {
                String error = "The chosen user is already your friend!";
                FrameHandler.getFrameHandler().showErrorMessage(error);
            }
        }
        else
        {
            errorLabel.setText("you must choose a user to add as a friend");
            errorLabel.setVisible(true);
        }
    }//GEN-LAST:event_addFriendActionPerformed

    private void removeFriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFriendActionPerformed
        if(!friendsList.isSelectionEmpty()){
            ButtonsFunctions.getButtonsFunctions().processRemoveFriend((String)friendsList.getSelectedValue());
            if(DataManager.getDataManager().getSuccessOrFailure() == DataManager.FAILURE)
                    return;
             refresh();
        }else{
            errorLabel.setText("you must choose a friend to remove");
            errorLabel.setVisible(true);
        }
    }//GEN-LAST:event_removeFriendActionPerformed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
       ButtonsFunctions.getButtonsFunctions().processGetForums();
       Vector<Frm> forums = DataManager.getDataManager().getVectorForums();
       int type = DataManager.getDataManager().getCurrTypeOfUser();
       FrameHandler.getFrameHandler().changeCurrentFrame(new ChooseForum(forums, type));
    }//GEN-LAST:event_backActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addFriend;
    private javax.swing.JList allUsers;
    private javax.swing.JButton back;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JList friendsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeFriend;
    // End of variables declaration//GEN-END:variables

    private void refresh() {
       ButtonsFunctions.getButtonsFunctions().processGetUsers();
       ButtonsFunctions.getButtonsFunctions().processGetFriends();
       Vector<User> users = DataManager.getDataManager().getVectorUser();
       Vector<User> friends = DataManager.getDataManager().getVectorFriends();
       FrameHandler.getFrameHandler().changeCurrentFrame(new ManageFriends(users,friends));
    }

}
